--
-- OpenServerGift.lua
-- Created by IntelliJ IDEA.
-- User: yao.chen
-- Date: 2019-01-24
-- 开服礼包
--

local GiftRenderForeign = import(".renders.GiftRenderForeign")
local ResRender = require("app.widget.renders.ResRender")

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.GiftNodeFour))
    sprite:init()
    return sprite
end

function ClassRef:init()
    self.super.init(self, data)

    self._resRender = ResRender.create(self._ui.Panel_money)
    self._resRender:setData({type = Enums.TipsType.daibi, id = Enums.PropType.coin, num = 0})

    local renders = {0, 0, 0, 0 }
    for i = 1, #renders do
        local render = GiftRenderForeign.create(self._ui["Panel/node_" .. i])
        render:onOperate(function(op, data)
            executeFunc(self._onOperate, op, data)
        end)
        render:setVisible(false)
        renders[i] = render
    end
    self._giftRender = renders
end

function ClassRef:refreshPanel(data, isRefreshSelect)
    if not isRefreshSelect and data[1] then
        local goods = data[1].goods
        for k, v in ipairs(self._giftRender) do
            v:setData(goods[k])
            v:setVisible(true)
        end
    else
        for _, v in ipairs(self._giftRender) do
            if data.id == v:getGiftsId() then
                v:setData(data)
            end
        end
    end
end

function ClassRef:getIsAllSellOut()
    local isSellOut = true
    for _, v in ipairs(self._giftRender) do
        local temp = v:getIsSellOut()
        if not temp then
            isSellOut = false
            break
        end
    end

    return isSellOut
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

function ClassRef:updateCoin(num)
    self._resRender:setNum(num)
end

return ClassRef
